<?php
/******************************
 * EQdkp
 * Copyright 2002-2005
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * listtriggers.php
 * Began: November 2007
 * 
 * 
 ******************************/
 
define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');

$user->check_auth('u_item_list');

//
// Item Request History (all items)
//
if ( $_GET[URI_PAGE] == 'history' )
{
    $sort_order = array(
	0 => array('trigger_name', 'trigger_name desc'),
	1 => array('trigger_holder', 'trigger_holder desc'),
	2 => array('raid_name', 'raid_name desc'),
	3 => array('raid_date', 'raid_date desc')
    );
    
    $current_order = switch_order($sort_order);

    $u_list_items = 'listtriggers.php'.$SID.'&amp;' . URI_PAGE . '=history&amp;';
    
    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'];
    
    $total_triggers = $db->query_first('SELECT count(*) FROM ' . TRIGGERS_TABLE);
    $start = ( isset($_GET['start']) ) ? $_GET['start'] : 0;
    
    $s_history = true;
    
    $sql = 'SELECT '.TRIGGERS_TABLE.'.*, '.RAIDS_TABLE.'.raid_name, '.RAIDS_TABLE.'.raid_id, '.RAIDS_TABLE.'.raid_date
	    FROM ' . TRIGGERS_TABLE . '
		LEFT JOIN ' . RAIDS_TABLE .'
		ON '.TRIGGERS_TABLE.'.raid_used_id='.RAIDS_TABLE.'.raid_id
	    ORDER BY '.$current_order['sql']. '
	    LIMIT '.$start.','.$user->data['user_ilimit'];

    $listitems_footcount = sprintf($user->lang['listpurchased_footcount'], $total_items, $user->data['user_ilimit']);
    $pagination = generate_pagination('listtriggers.php'.$SID.'&amp;' . URI_PAGE . '=history&amp;o='.$current_order['uri']['current'], 
                                       $total_triggers, $user->data['user_ilimit'], $start);
}

//
// Open Item Requests (not fulfilled)
//
else
{
    $sort_order = array(
        0 => array('trigger_name', 'trigger_name desc'),
        1 => array('trigger_holder', 'trigger_holder desc'),
        2 => array('raid_name', 'raid_name desc')
    );

    $current_order = switch_order($sort_order);

    $u_list_items = 'listtriggers.php'.$SID.'&amp;';

    $page_title = sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']).': '.$user->lang['listpurchased_title'];

    $total_triggers = $db->query_first('SELECT count(*) FROM ' . TRIGGERS_TABLE . ' WHERE raid_used_id IS NULL');
    $start = ( isset($_GET['start']) ) ? $_GET['start'] : 0;

    $s_history = false;

    $sql = 'SELECT trigger_name, trigger_id, trigger_holder
	    FROM ' . TRIGGERS_TABLE . '
	    WHERE raid_used_id IS NULL
	    ORDER BY '.$current_order['sql']. '
	    LIMIT '.$start.','.$user->data['user_ilimit'];
    $listitems_footcount = sprintf($user->lang['listpurchased_footcount'], $total_items, $user->data['user_ilimit']);
    $pagination = generate_pagination('listtriggers.php'.$SID.'&amp;o='.$current_order['uri']['current'],
                                       $total_items, $user->data['user_ilimit'], $start);

}

// Regardless of which listitem page they're on, we're essentially 
// outputting the same stuff. Request History just has a buyer column.
if ( !($triggers_result = $db->query($sql)) )
{
    message_die('Could not obtain trigger information', '', __FILE__, __LINE__, $sql);
}

while ( $trigger = $db->fetch_record($triggers_result) )
{
    $tpl->assign_block_vars('triggers_row', array(
        'ROW_CLASS' => $eqdkp->switch_row_class(),
	'TRIGGER'   => stripslashes($trigger['trigger_name']),
	'HOLDER'    => ( !empty($trigger['trigger_holder']) ) ? $trigger['trigger_holder'] : '&lt;<i>Not Found</i>&gt;',
	'RAID'	    => ( !empty($trigger['raid_name']) ) ? $trigger['raid_name'] : '',
        'RAID_DATE' => ( !empty($trigger['trigger_date']) ) ? date($user->style['date_notime_short'], $trigger['trigger_date']) : '&nbsp;',
        'U_VIEW_RAID' => 'viewraid.php'.$SID.'&amp;' . URI_RAID . '='.$trigger['raid_id'],
	'U_VIEW_HOLDER' => 'viewmember.php'.$SID.'&amp;' . URI_NAME . '='.$trigger['trigger_holder'])
    );
}
$db->free_result($items_result);

$tpl->assign_vars(array(
    'L_TRIGGER' => $user->lang['trigger'],
    'L_HOLDER' => $user->lang['holder'],
    'L_RAID' => $user->lang['raid_used'],
    'L_DATE' => $user->lang['date'],

    'O_TRIGGER' => $current_order['uri'][0],
    'O_HOLDER' => $current_order['uri'][1],
    'O_RAID' => $current_order['uri'][2],
    'O_DATE' => $current_order['uri'][3],
    
    'U_LIST_TRIGGERS' => $u_list_items,
    
    'START' => $start,
    'S_HISTORY' => $s_history,
    'LISTITEMS_FOOTCOUNT' => $listitems_footcount,
    'ITEM_PAGINATION' => $pagination)
);

$eqdkp->set_vars(array(
    'page_title'    => $page_title,
    'template_file' => 'listtriggers.html',
    'display'       => true)
);
?>
